Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind trait props/consts before parent class #15878

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

iluuu1994
Copy link
Member

This more accurately matches the "copy & paste" semantics described in the documentation. The same could be done for methods. However, abstract trait methods diverge from this behavior, given that a parent method can satisfy trait methods used in the child. In that case, the method is not copied, but the check must be performed after the parent has been bound. For now, just bother with properties and constants.

Fixes GH-15753

I'm unsure if this should be backported. It might be safer to just commit for master.

Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't know if this change is OK or not and what subsequences it may make.

Comment on lines -18 to +20
trait TestTrait2 {
public final const Constant = 123;
}

class BaseClass2 {
public final const Constant = 456;
}

class DerivedClass2 extends BaseClass2 {
use TestTrait2;
}

?>
--EXPECTF--
Fatal error: BaseClass2 and TestTrait2 define the same constant (Constant) in the composition of DerivedClass2. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
Fatal error: DerivedClass1::Constant cannot override final constant BaseClass1::Constant in %s on line %d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a behavior change, that may break some PHP code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. This is (IMO) clearly a bug, but it may be better to commit to master anyway.

@@ -0,0 +1,30 @@
--TEST--
GH-15753: Overriding readonly properties from traits don't allow assignment from the child
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test works fine on unpatched master, but fails on PHP-8.2. Is this a backport?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this works on master because of the changes to readonly in https://wiki.php.net/rfc/asymmetric-visibility-v2#relationship_with_readonly. Namely, readonly properties can, by default, now be initialized from all sub-classes. For master, we should add a better test.

@cmb69
Copy link
Member

cmb69 commented Oct 8, 2024

A couple of weeks ago I would have suggested to apply this master only, but now I'm unsure whether it should still go into PHP-8.4. Maybe the @php/release-managers-84 want to have a look.

@NattyNarwhal
Copy link
Member

I'm also unsure about this myself. This late and this ambiguous, I think it'd be better off in 8.5.

@iluuu1994
Copy link
Member Author

Can you explain what you mean by ambiguous? It might be good to adjust methods too, which should make behavior consistent and predictable. But it might require a bit more changes.

@NattyNarwhal
Copy link
Member

I was referring to Dmitry's comment about possible fallout.

@iluuu1994
Copy link
Member Author

Ok, thanks for the clarification. I will make the changes for methods, and then target this to master.

This more accurately matches the "copy & paste" semantics described in the
documentation. The same could be done for methods. However, abstract trait
methods diverge from this behavior, given that a parent method can satisfy trait
methods used in the child. In that case, the method is not copied, but the check
must be performed after the parent has been bound. For now, just bother with
properties and constants.

Fixes phpGH-15753
@iluuu1994 iluuu1994 changed the base branch from PHP-8.2 to master October 14, 2024 17:44
* Don't iterate trait methods if they don't contain an abstract method
* Don't build the exclude and alias table twice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Collision of interface constant and trait constant should not produce fatal error
4 participants